package com.hearwego;

import java.util.List;
import com.utilities.HWGSQLiteHelper;
import com.utilities.Informacion;
import com.utilities.RssParserPull;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ActivityDetalle extends FragmentActivity {
	
	Button back;
	SharedPreferences preferences;
	SharedPreferences.Editor editor;
	int actualId;
	ProgressDialog dialog;
	List<Informacion> informacion;
	HWGSQLiteHelper usdbh;
	SQLiteDatabase db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detalle);
		
		preferences= getSharedPreferences("Preferencias",Context.MODE_PRIVATE);
		editor = preferences.edit();
		
		actualId = preferences.getInt("actualid", 0);
		//Toast.makeText(this, "id on TabDetalle "+actualId, Toast.LENGTH_LONG).show();
		usdbh =new HWGSQLiteHelper(this, "DBHWG", null, 1);
        db = usdbh.getWritableDatabase();
        
        back = (Button) findViewById(R.id.back);
        back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onBackPressed();
				String deletDetalle = "DELETE FROM Detalle";
				db.execSQL(deletDetalle);
				String deletAudio = "DELETE FROM Audio";
				db.execSQL(deletAudio);
			}
		});
		
	}
	
	class getInfoHeader extends AsyncTask<String, String, String>{
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = ProgressDialog.show(ActivityDetalle.this, "","Cargando. Por favor espere...", true);
		}

		@Override
		protected String doInBackground(String... params) {
			Log.i("inicio", "inicio");
			RssParserPull parse = new RssParserPull("http://www.smartthinking.com.mx:8080/HearWeGo/DataServlet?Query=select%20*%20from%20pois%20where%20poi_idr=+"+actualId+"+&BaseDatos=hwg");
			informacion = null;
			informacion = parse.parse();
			Log.i("fin", "fin");
	        
	        if(db != null)
	        {
	        	
	          for(int i=0; i < informacion.size();i++){
	        	  db.execSQL("INSERT INTO Detalle (poi_idr, poi_nombre, poi_foto, poi_direccion, poi_metro, poi_metrobus, poi_servicios," +
		        			"poi_clasificacion, poi_www, poi_mail, poi_tel, poi_lat, poi_lng, poi_idioma, poi_ruta, poi_horario, " +
		        			"poi_costo, poi_descripcion, poi_cuentaporcentaje, poi_entradalibre, poi_encabezado) " +
	                           "VALUES ('" +informacion.get(i).getId().toString()+"', '"+informacion.get(i).getNombre()+"', '"+informacion.get(i).getFoto()+"'," +
	                           		"'"+informacion.get(i).getDireccion()+"', '"+informacion.get(i).getMetro()+"', '"+informacion.get(i).getMetroBus()+"'," +
	                           		"'"+informacion.get(i).getServicios()+"', '"+informacion.get(i).getClacificacion()+"', '"+informacion.get(i).getWww()+"'," +
	                           		"'"+informacion.get(i).getMail()+"','"+informacion.get(i).getTel()+"', '"+informacion.get(i).getLat()+"', '"+informacion.get(i).getLng()+"'," +
	                           		"'"+informacion.get(i).getIdioma()+"', '"+informacion.get(i).getRuta()+"', '"+informacion.get(i).getHorario()+"'," +
	                           		"'"+informacion.get(i).getCosto()+"', '"+informacion.get(i).getDescripcionPoi()+"', '"+informacion.get(i).getCuenta_porcentaje()+"', '"+informacion.get(i).getEntradaLibre()+"'," +
	                           		"'"+informacion.get(i).getEncabezado()+"')");
					Toast.makeText(getApplicationContext(), "Nota agregada", Toast.LENGTH_SHORT).show();
					dialog.dismiss();
		            //Cerramos la base de datos
		            db.close();
	          }	
	        	
	        }
 			
			return null;
		}
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
		}
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		String deletDetalle = "DELETE FROM Detalle";
		db.execSQL(deletDetalle);
		String deletAudio = "DELETE FROM Audio";
		db.execSQL(deletAudio);
	}
	

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		getMenuInflater().inflate(R.menu.activity_detalle, menu);
//		return true;
//	}

}
